<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
// use Mrgoon\AliSms\AliSms;
use App\Models\SmsLogModel;
use App\Services\MemberService;
use App\Services\CommonService;
use Session;

class RegisterController extends Controller{
    public function getToken(){
        $common = new CommonService;
        $common_info = $common->getRandomToken();
        if($common_info){
            $_data = array('code'=>'2x000001','_token'=>$common_info);
        }else{
            $_data = array('code'=>'4x000001','msg'=>'暂无信息');
        }

        return response()->json($_data);
    }

    public function getWechatCode(){
        $canshu = "";
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize";
        $canshu = $canshu . "?appid=wx8bce72636ba79f5e";
        $canshu = $canshu . "&redirect_uri=".urlencode("http://boomdog.madao100.com/wechat/get_openid");
        $canshu = $canshu . "&response_type=code";
        $canshu = $canshu . "&scope=snsapi_userinfo";
        $canshu = $canshu . "&state=123#wechat_redirect";
        $url = $url . $canshu;

        header("Location:$url");
    }

    public function getOpenid($type='couese'){
        $code = $_REQUEST['code'];

        $url_token = "";
        $url_token = $url_token.'https://api.weixin.qq.com/sns/oauth2/access_token';
        $url_token = $url_token.'?appid=wx8bce72636ba79f5e&secret=c98e8c75b8a31765e6745b8cc24a4180';
        $url_token = $url_token.'&code='.$code;
        $url_token = $url_token.'&grant_type=authorization_code';

        $token = file_get_contents($url_token);
        $token = json_decode($token,1);

        $access_token = $token['access_token'];
        $openid = $token['openid'];

        $url_userinfo = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid="
            .$openid."&lang=zh_CN";
        $userinfo = file_get_contents($url_userinfo);
        $userinfo = str_replace('\\','',$userinfo);

        if($userinfo != ''){
            if($type == 'course'){
                header('Location:http://boom.madao100.com/?userinfo='.base64_encode($userinfo).'&code='.$code.'#/login');
            }else{
                header('Location:http://boom.madao100.com/?userinfo='.base64_encode($userinfo).'&code='.$code.'#/WxAuth');
            }
        }
    }

    public function getcode($telephone){
		$response = SmsLogModel::sendVerifyCode($telephone);
		if($response === true){
			$_data = array('code'=>200,'msg'=>'短信验证码获取成功');
		}else{
			$_data = array('code'=>400,'msg'=>'短信验证码获取失败');
		}

		return response()->json($_data);
	}

	public function register(){
		date_default_timezone_set('PRC');
		$member = new MemberService;

		$data['telephone'] = addslashes($_POST['telephone']);
		$data['isbuy'] = 0;
		$data['check'] = 'normal';
		$data['nick'] = $_POST['nick'] ? addslashes($_POST['nick']) : '';
		$data['thumb'] = $_POST['thumb'] ? addslashes($_POST['thumb']) : '';
		$data['register_date'] = date('Y-m-d H:i:s');
		$data['openid'] = addslashes($_POST['openid']);

//		$code = $_POST['code'] ? intval($_POST['code']) : 475861;

		$info = $member->getMemberInfo($data['telephone']);
		if(!empty($info)){
//		    if($code != ''){
//		        SmsLogModel::checkCode($data['telephone'],$code);
//            }
			$_data = array('code'=>'3x000001','msg'=>'用户已存在,请勿重复注册');
		}else{
			$res = $member->insertMember($data);
			if($res){
			    Session::put('id',$res);
			    $userinfo = array('telephone'=>$data['telephone'],'nick'=>$data['nick'],'thumb'=>$data['thumb'],'openid'=>$data['openid']);
				$_data = array('code'=>'2x000001','msg'=>'用户注册成功','userid'=>Session::get('id'),'userinfo'=>$userinfo);
			}else{
				$_data = array('code'=>'4x000001','msg'=>'用户注册失败');
			}
		}

		return response()->json($_data);
	}

	public function login(){
        date_default_timezone_set('PRC');
		$member = new MemberService;
		$telephone = addslashes($_POST['telephone']);
//		$code = intval($_POST['code']);

		$memberinfo = $member->getMemberInfo($telephone);
		if(!empty($memberinfo) && $memberinfo->check != 'disable'){
//		    if($code){
//		        SmsLogModel::checkCode($telephone,$code);
//            }

			$userinfo = array();
			$userinfo['id'] = $memberinfo->id;
			$userinfo['telephone'] = $memberinfo->telephone;
			$userinfo['nick'] = $memberinfo->nick;
			$userinfo['thumb'] = $memberinfo->thumb;
			if($memberinfo->isbuy == 0){
				$userinfo['isbuy'] = '未购买教具';
			}else{
				$userinfo['isbuy'] = '已购买教具';
			}
			$userinfo['islogin'] = 'yes';
			$userinfo['openid'] = $memberinfo->openid;
			Session::put('userinfo',$userinfo);

			$data['login_time'] = date('Y-m-d H:i:s');
			$member->modifyMember($memberinfo->telephone,$data);

			$_data = array('code'=>'2x000001','msg'=>'登录成功','userinfo'=>Session::get('userinfo'));
		}else{
			$_data = array('code'=>'4x000003','msg'=>'暂无用户信息，请注册');
		}

		return response()->json($_data);
	}
}